package me.zhengjie.modules.website.repository;


import me.zhengjie.modules.website.domain.DocumentFile;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author system
 * @since 2024-11-06 15:51:43
 */
@Component
public class DocumentFileSpecifications {

    public static Specification<DocumentFile> queryList(DocumentFile documentFile) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (documentFile.getId() != null) {
                predicates.add(builder.like(root.get("id" ).as(String.class), "%" + documentFile.getId() + "%" ));
            }
            if (documentFile.getFileName() != null) {
                predicates.add(builder.like(root.get("fileName" ).as(String.class), "%" + documentFile.getFileName() + "%" ));
            }
            if (documentFile.getPublishTime() != null) {
                predicates.add(builder.equal(root.get("publishTime" ).as(Date.class), documentFile.getPublishTime()));
            }
            if (documentFile.getRevisionTime() != null) {
                predicates.add(builder.equal(root.get("revisionTime" ).as(Date.class), documentFile.getRevisionTime()));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}

